﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServisniList.Core;

namespace ServisniList
{
    public partial class OpisRadaServiserOdabir : Form
    {

        #region Properties

        public string Odabir { get; set; }
        private DataModelDataContext context = null;

        #endregion

        #region Constructor

        public OpisRadaServiserOdabir()
        {
            InitializeComponent();
            context = new DataModelDataContext(ConfigurationManager.ConnectionStrings["ServisniListConnectionString"].ConnectionString);
            odabraniOpisiRadaBS.DataSource =
            (
                    from item in context.OpisRadas
                    select new OdabraniOpisiRadaVizual() 
                    { 
                    Id = item.Id,
                    Opis = item.Opis                
                    }
            ).ToList();
        }

        #endregion

        #region Event handlers

        private void btnDodajNovi_Click(object sender, EventArgs e)
        {
            OpisRadaNovi noviOpis = new OpisRadaNovi();
            noviOpis.ShowDialog();
            if (noviOpis.UspjesnoKreiranNovi)
            {
                odabraniOpisiRadaBS.DataSource =
                (
                    from item in context.OpisRadas
                    select new OdabraniOpisiRadaVizual()
                    {
                        Id = item.Id,
                        Opis = item.Opis
                    }
                ).ToList();
            }
        }

        private void btnOdaberi_Click(object sender, EventArgs e)
        {
            List<Core.OdabraniOpisiRadaVizual> odaraniOpisiRada = (List<Core.OdabraniOpisiRadaVizual>)odabraniOpisiRadaBS.DataSource;

            foreach (var item in odaraniOpisiRada)
            {
                if (item.Odaberi)
                {
                    Odabir += "-" + item.Opis + Environment.NewLine;
                }
            }
            this.Close();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }

        private void OpisRada_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (context != null)
            {
                context.Dispose();
            }
        }

        #endregion
    }
}
